package com.ruoyi.web.controller.farmer;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.farmer.domain.Variety;
import com.ruoyi.farmer.service.IVarietyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 农产品品种  Controller
 *
 * @author 姚毫征
 * @date 2023-09-14
 */
@RestController
@RequestMapping("/variety/variety")
public class VarietyController extends BaseController
{
    @Autowired
    private IVarietyService varietyService;

    /**
     * 查询农产品品种  列表
     */
    @PreAuthorize("@ss.hasPermi('variety:variety:list')")
    @GetMapping("/list")
    public TableDataInfo list(Variety variety)
    {
        startPage();
        List<Variety> list = varietyService.selectVarietyList(variety);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('variety:variety:getcountspe')")
    @GetMapping(value = "/getcountspe")
    public AjaxResult getcountspe()
    {
        return success(varietyService.getcountspe());

    }

    /**
     * 导出农产品品种  列表
     */
    @PreAuthorize("@ss.hasPermi('variety:variety:export')")
    @Log(title = "农产品品种  ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Variety variety)
    {
        List<Variety> list = varietyService.selectVarietyList(variety);
        ExcelUtil<Variety> util = new ExcelUtil<Variety>(Variety.class);
        util.exportExcel(response, list, "农产品品种  数据");
    }

    /**
     * 获取农产品品种  详细信息
     */
    @PreAuthorize("@ss.hasPermi('variety:variety:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(varietyService.selectVarietyById(id));
    }
    //获取所有品种
    @PreAuthorize("@ss.hasPermi('variety:variety:getAllVariety')")
    @GetMapping(value = "/getAllVariety/{speciesId}")
    public AjaxResult getAllVariety(@PathVariable("speciesId") String speciesId)
    {
        System.out.println(speciesId);
        return success(varietyService.getAllVariety(speciesId));
    }

    /**
     * 新增农产品品种
     */
    @PreAuthorize("@ss.hasPermi('variety:variety:add')")
    @Log(title = "农产品品种  ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Variety variety)
    {
        variety.setVarietyId(UUID.randomUUID().toString());
        variety.setCreateTime(DateUtils.getTime());
        return toAjax(varietyService.insertVariety(variety));
    }

    /**
     * 修改农产品品种
     */
    @PreAuthorize("@ss.hasPermi('variety:variety:edit')")
    @Log(title = "农产品品种  ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Variety variety)
    {
        variety.setUpdateTime(DateUtils.getTime());
        return toAjax(varietyService.updateVariety(variety));
    }

    /**
     * 删除农产品品种
     */
    @PreAuthorize("@ss.hasPermi('variety:variety:remove')")
    @Log(title = "农产品品种  ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(varietyService.deleteVarietyByIds(ids));
    }
}
